//
//  GFBigFoodImageViewController.m
//  GoodFood
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFBigFoodImageViewController.h"

@interface GFBigFoodImageViewController ()

@property (nonatomic, strong) UIBlurEffect *blurEffect;
@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) UIImageView *centerImage;
@property (nonatomic, assign) NSInteger num;

@end

@implementation GFBigFoodImageViewController

-(void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.waitView removeFromSuperview];
    
    self.num = arc4random()%100;
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [image setImageWithURL:[NSURL URLWithString:self.url] placeholderImage:nil];
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    
    self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:self.blurEffect];
    self.effectView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    
    [self.view addSubview:self.effectView];
    
    
    [self performSelector:@selector(delay) withObject:nil afterDelay:0.5];
    
}

-(void)delay {
    
    if (self.num % 2 == 0) {
        self.centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 3 * 2, ScreenWidth)];
        self.centerImage.layer.cornerRadius = 7;
        self.centerImage.layer.masksToBounds = YES;
        self.centerImage.userInteractionEnabled = YES;
        
//            [self.centerImage setTransform:CGAffineTransformMakeRotation(M_PI / 180 * 10)];
        self.centerImage.center = self.view.center;
        
        CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        keyFrame.delegate = self;
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathMoveToPoint(path, NULL, ScreenWidth / 2, 0);
        
        
        CGPathAddLineToPoint(path, NULL, self.view.center.x, self.view.center.y + 50);
        keyFrame.path = path;
        keyFrame.duration = 0.3;
        [self.centerImage setImageWithURL:[NSURL URLWithString:self.url] placeholderImage:nil];
        [self.centerImage.layer addAnimation:keyFrame forKey:@"keyFrame"];
        [self.view addSubview:self.centerImage];

    } else {
        
        
        self.centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 3 * 2, ScreenWidth)];
        self.centerImage.layer.cornerRadius = 7;
        self.centerImage.layer.masksToBounds = YES;
        self.centerImage.userInteractionEnabled = YES;
        [self.centerImage setImageWithURL:[NSURL URLWithString:self.url] placeholderImage:nil];
        //    [self.centerImage setTransform:CGAffineTransformMakeRotation(M_PI / 180 * 0.5)];
        self.centerImage.center = self.view.center;
        [self.view addSubview:self.centerImage];
        CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        
        /* 设置值得范围, fromValue toValue */
        basic.fromValue = [NSNumber numberWithInt:0.3];
        basic.toValue = [NSNumber numberWithFloat:1];
        /* 设置相关动画属性 */
        basic.duration = 0.8;
        basic.repeatCount = 0;
        
        /* 将动画添加在layer上 */
        [self.centerImage.layer addAnimation:basic forKey:@"basic"];

        [self performSelector:@selector(delayForButton) withObject:nil afterDelay:0.8];
        
        
    }
    
    
    
}

-(void)delayForButton {

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, ScreenWidth / 10, ScreenWidth / 10);
    button.center = self.centerImage.frame.origin;
    
    [button setImage:[[UIImage imageNamed:@"iconfont-chahao"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
 
    
}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    
    
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.view.center.x, self.view.center.y + 50);
    keyFrame.removedOnCompletion = NO;
//    [self.centerImage setTransform:CGAffineTransformMakeRotation(M_PI / 180 * 360)];
    CGPathAddLineToPoint(path, NULL, self.view.center.x, self.view.center.y);
    keyFrame.path = path;
    keyFrame.duration = 0.15;
    [self.centerImage.layer addAnimation:keyFrame forKey:@"keyFrames"];
    [self.view addSubview:self.centerImage];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, ScreenWidth / 10, ScreenWidth / 10);
    button.center = self.centerImage.frame.origin;
    
    [button setImage:[[UIImage imageNamed:@"iconfont-chahao"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];

    
    
}

-(void)button:(UIButton *)button {
    
    self.navigationController.navigationBarHidden = NO;
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
